import tkinter as tk
from tkinter import ttk
from typing import Callable
from .chat_tab import ChatTab
from .settings_tab import SettingsTab
from .history_tab import HistoryTab
# 新增导入
from .discussion_tab import DiscussionTab
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from models.factory import ModelFactory

class MainWindow:
    def __init__(self, root, config_manager=None):
        self.root = root
        self.config_manager = config_manager
        # 从ModelFactory获取模型配置
        self.models = ModelFactory.get_model_configs()
        
        # 回调函数
        self.on_send = None
        self.on_model_change = None
        self.on_thinking_change = None
        self.on_save_settings = None
        self.on_clear_chat = None
        self.on_role_change = None
        self.on_export_history = None
        self.on_import_history = None
        self.on_clear_history = None
        self.on_load_conversation = None
        self.on_delete_conversation = None
        
        self.setup_ui()
    
    def setup_ui(self):
        """设置主界面"""
        self.root.title("AI聊天助手")
        self.root.geometry("900x700")
        
        # 创建标签页控件
        self.notebook = ttk.Notebook(self.root)
        self.notebook.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
        
        # 设置标签页
        self.setup_tabs()
    
    def setup_tabs(self):
        """设置标签页"""
        # 聊天标签页
        self.chat_frame = ttk.Frame(self.notebook)
        self.notebook.add(self.chat_frame, text="聊天")
        self.chat_tab = ChatTab(self.chat_frame, self.models, self.config_manager)
        
        # 设置标签页
        self.settings_frame = ttk.Frame(self.notebook)
        self.notebook.add(self.settings_frame, text="设置")
        self.settings_tab = SettingsTab(self.settings_frame, self.models, self.config_manager)
        
        # 历史标签页
        self.history_frame = ttk.Frame(self.notebook)
        self.notebook.add(self.history_frame, text="历史")
        self.history_tab = HistoryTab(self.history_frame)
        
        # 讨论标签页
        self.discussion_frame = ttk.Frame(self.notebook)
        self.notebook.add(self.discussion_frame, text="讨论")
        self.discussion_tab = DiscussionTab(self.discussion_frame)
    
    def setup_tab_callbacks(self):
        """设置标签页的回调函数"""
        # 设置聊天标签页的回调
        self.chat_tab.set_callbacks(
            on_send=self.on_send,
            on_model_change=self.on_model_change,
            on_thinking_change=self.on_thinking_change,
            on_clear_chat=self.on_clear_chat,
            on_role_change=self.on_role_change
        )
        
        # 设置设置标签页的回调
        self.settings_tab.set_callbacks(
            on_model_change=self.on_model_change,
            on_save_settings=self.on_save_settings,
            on_role_change=self.on_role_change
        )
        
        # 设置历史标签页回调
        self.history_tab.set_callbacks(
            on_load_conversation=self.on_load_conversation,
            on_delete_conversation=self.on_clear_history
        )
    
    def set_callbacks(self,
                      on_send: Callable[[str], None],
                      on_model_change: Callable[[], None],
                      on_thinking_change: Callable[[], None],
                      on_save_settings: Callable[[], None],
                      on_clear_chat: Callable[[], None],
                      on_role_change: Callable[[], None] = None,
                      on_export_history: Callable[[], None] = None,
                      on_import_history: Callable[[], None] = None,
                      on_clear_history: Callable[[], None] = None,
                      on_load_conversation: Callable = None,
                      on_delete_conversation: Callable = None):
        """设置回调函数"""
        self.on_send = on_send
        self.on_model_change = on_model_change
        self.on_thinking_change = on_thinking_change
        self.on_save_settings = on_save_settings
        self.on_clear_chat = on_clear_chat
        self.on_role_change = on_role_change
        self.on_export_history = on_export_history
        self.on_import_history = on_import_history
        self.on_clear_history = on_clear_history
        self.on_load_conversation = on_load_conversation
        self.on_delete_conversation = on_delete_conversation
        
        # 更新标签页的回调函数
        self.setup_tab_callbacks()
    
    def get_settings(self):
        """获取设置"""
        return self.settings_tab.get_settings()
    
    def load_settings(self, settings):
        """加载设置"""
        self.settings_tab.load_settings(settings)
        # 同步模型选择到聊天标签页
        if 'model' in settings:
            self.chat_tab.set_model(settings['model'])
    
    def get_input_text(self):
        """获取输入文本"""
        return self.chat_tab.get_input_text()
    
    def clear_input(self):
        """清空输入框"""
        self.chat_tab.clear_input()
    
    def add_message(self, sender, message, thinking_content=None):
        """添加消息"""
        self.chat_tab.add_message(sender, message, thinking_content)
    
    def clear_chat(self):
        """清空聊天"""
        self.chat_tab.clear_chat()
    
    def update_history_display(self):
        """更新历史显示"""
        # 刷新历史记录列表
        self.history_tab.refresh_history()
    
    def set_history_data(self, history_data):
        """设置历史数据"""
        self.history_tab.set_history_data(history_data)
    
    def get_model_var(self):
        """获取模型变量"""
        return self.chat_tab.get_model_var()
    
    def get_thinking_var(self):
        """获取深度思考变量"""
        return self.chat_tab.get_thinking_var()
    
    def get_role_var(self):
        """获取角色变量"""
        return self.chat_tab.get_role_var()
    
    def set_typing_status(self, is_typing):
        """设置打字状态"""
        self.chat_tab.set_typing_status(is_typing)
    
    def initialize_chat_model(self):
        """初始化聊天模型"""
        self.chat_tab.initialize_model()